<template>
  <div>
    <div class="header">
      <div class="column">证券简称</div>
      <div class="column right">上市日期</div>
    </div>
    <div style="width: 100% ;height: 180px; background-color:rgb(5, 25, 75) "
         @mouseenter="stopAutoPaging"
         @mouseleave="startAutoPaging">
      <div v-for="item in tableData" :key="item.id">
        <div class="content">
          <div class="stock stockbox">{{item.stockAbbreviation}}</div>
          <div class="stockbox">
            <div class="stockdata">{{ item.listingDate }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="block">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[4, 8, 12, 16]"
        :page-size="params.pageSize"
        layout="prev, pager, next"
        :total="total">
    </el-pagination>
  </div>
</template>
<script>
import needTools from "@/utils/needTools.js";
export default {
  name: "Calendar",
  data() {
    return {
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 4,
      },
      total: 0,
      pagetotal: 0,
      currentPage: 0,
      autoLoadTimerId: null, // 用于存储每10秒更新数据的定时器ID
      autoPagingTimerId: null // 用于存储自动翻页的定时器ID
    }
  },
  created() {
    this.load();
    this.startAutoPaging();
    // 设置定时器，每10秒更新数据
    this.autoLoadTimerId = setInterval(() => {
      this.load();
    }, 10000);
  },
  beforeDestroy() {
    clearInterval(this.autoLoadTimerId);
    clearInterval(this.autoPagingTimerId);
  },
  methods:{
    load(){
      needTools.get("/calendar/list",{
        params: this.params
      }).then(res=>{
        if(res.code === 200){
          this.tableData =res.data.records;
          this.total = res.data.total;
          this.pagetotal = Math.ceil(this.total / 4);
          this.currentPage = this.params.pageNum;
          console.log(this.currentPage);
        }else {
          this.$message.error(res.msg);
        }
      })
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum){
      this.params.pageNum=pageNum;
      this.load();
    },
    startAutoPaging() {
      this.autoPagingTimerId = setInterval(() => {
        // 假设你想要在翻到最后一页后回到第一页
        if (this.params.pageNum >= this.pagetotal) {
          this.params.pageNum = 1;
        } else {
          this.params.pageNum++;
        }
        this.load();
      }, 3000);
    },
    stopAutoPaging() {
      clearInterval(this.autoPagingTimerId);
    },
  }
}
</script>
<style scoped>
.column, .column right{
  color: #bbbdc6;
  font-size: 14px;
  display: inline-block;
  width: 50%;
  height: 25px;
  line-height: 25px;
  background-color: rgba(4, 59, 154,0.2);
  margin-bottom: 5px;
  margin-top:4px;
}

.column{
  padding-left: 25px;
}

.column right{
  text-align: right;
  padding-right: 35px;
}

.content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px;
  background-color: rgba(4, 59, 154, 0.2);
  color: #bbbdc6;
  font-size: 14px;
}

.stockbox {
  flex: 0 0 50%;
  padding: 0 10px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  height: 36px;
}


.stockdata {
  flex: 1;
  margin-right: 20px;
  color: #2aaee9;
  text-align: right;
}

:deep(.el-pagination)  {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

:deep(.el-pagination button),
:deep(.el-pagination ul li) {
  background: rgb(5, 23, 77);
  color: white;
}

:deep(.el-pagination .is-first),
:deep(.el-pagination .is-last) {
  background-color: rgb(5, 23, 77) !important;
  color: white;
  cursor: pointer;
}
</style>